[
    {
        "id": "ef206fcd63fc785c",
        "type": "subflow",
        "name": "Sum inputs",
        "info": "Subflow that can be used for totalizing\ndifferint inputs. A use case can be if\nyou want to show the total of three\ninput phases.\n\n# description\n\nA typical input is linking three phases\nof power to the input, all with its own\ntopic.\n\nThe inputs are cached, based on `msg.topic`\nand are summed as output.\n\n# configuration\n\nThe output _rate_ defaults to once per 5\nseconds, but that can be changed in the _edit\npanel_. Note that you cannot output faster\nthan the inputs get injected.\n\nIf you check the _Show only changed values_\ncheckbox, only values that are different than\nthe previous sum will be outputted.\n\nThe _expected_ is the number of inputs with different\ntopics that need to be seen before the node starts outputting data. If set to `0`, this field is ignored\nand output is send on first input.\n\nThe _topic_ can be used to set the topic.\n\n# cache\n\nIf you want to delete a value from the\ncache, you can input a message\nwith the `msg.payload` set to `delete`\nand the `msg.topic` send to the item\nthat you want to delete.",
        "category": "",
        "in": [
            {
                "x": 40,
                "y": 80,
                "wires": [
                    {
                        "id": "ab00184d6b31cf39"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 600,
                "y": 80,
                "wires": [
                    {
                        "id": "9cacac0ef42a925a",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "rate",
                "type": "num",
                "value": "5",
                "ui": {
                    "icon": "font-awesome/fa-clock-o",
                    "label": {
                        "en-US": "Time between messages (s)"
                    },
                    "type": "input",
                    "opts": {
                        "types": [
                            "num"
                        ]
                    }
                }
            },
            {
                "name": "onlychanges",
                "type": "bool",
                "value": "true",
                "ui": {
                    "icon": "font-awesome/fa-exchange",
                    "label": {
                        "en-US": "Show only changed values"
                    },
                    "type": "checkbox"
                }
            },
            {
                "name": "topic",
                "type": "str",
                "value": "Sum inputs",
                "ui": {
                    "icon": "font-awesome/fa-file-text-o",
                    "label": {
                        "en-US": "Topic"
                    },
                    "type": "input",
                    "opts": {
                        "types": [
                            "str"
                        ]
                    }
                }
            },
            {
                "name": "expected",
                "type": "num",
                "value": "3",
                "ui": {
                    "icon": "font-awesome/fa-bullseye",
                    "label": {
                        "en-US": "Expected inputs"
                    },
                    "type": "input",
                    "opts": {
                        "types": [
                            "num"
                        ]
                    }
                }
            }
        ],
        "meta": {
            "module": "Total (grid)",
            "version": "0.0.2",
            "author": "Dirk-Jan Faber <dfaber@victronenergy.com>",
            "desc": "Totals the inputs, based on different msg.topic strings",
            "keywords": "total, grid power",
            "license": "MIT"
        },
        "color": "#f7ab3e"
    },
    {
        "id": "ab00184d6b31cf39",
        "type": "function",
        "z": "ef206fcd63fc785c",
        "name": "Sum inputs on topic",
        "func": "let stored = flow.get(node.id+'stored') || {}\nlet previousSum = flow.get(node.id+'previousSum')\n\nif (Number(msg.payload)) {\n    stored[msg.topic] = msg.payload\n} else {\n    if (msg.payload === 'delete' && stored[msg.topic]) {\n        delete stored[msg.topic]\n    }\n}\n\nflow.set(node.id+'stored', stored)\nmsg.payload = Object.keys(stored).reduce((sum, key) => sum + parseFloat(stored[key] || 0), 0);\nif (env.get('onlychanges') && ( previousSum === msg.payload )) {\n    return\n}\nflow.set(node.id+'previousSum', msg.payload)\nmsg.rate = (Number(env.get('rate')) || 5) * 1000 \n\nmsg.topic = env.get('topic')\n\nif (env.get('expected') > 0 && Object.keys(stored).length < env.get('expected')) {\n    return\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 210,
        "y": 80,
        "wires": [
            [
                "9cacac0ef42a925a"
            ]
        ]
    },
    {
        "id": "9cacac0ef42a925a",
        "type": "delay",
        "z": "ef206fcd63fc785c",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "5",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": true,
        "outputs": 1,
        "x": 460,
        "y": 80,
        "wires": [
            []
        ]
    },
    {
        "id": "f58786176192b1b7",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "eb8bf3be4bd80ae7",
        "type": "subflow:ef206fcd63fc785c",
        "z": "f58786176192b1b7",
        "name": "",
        "env": [
            {
                "name": "onlychanges",
                "type": "bool",
                "value": "false"
            }
        ],
        "x": 630,
        "y": 260,
        "wires": [
            [
                "61de96920fc889e1"
            ]
        ]
    },
    {
        "id": "61de96920fc889e1",
        "type": "debug",
        "z": "f58786176192b1b7",
        "name": "Total grid power",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 840,
        "y": 260,
        "wires": []
    },
    {
        "id": "a74e729465f1468e",
        "type": "victron-input-system",
        "z": "f58786176192b1b7",
        "service": "com.victronenergy.system/0",
        "path": "/Ac/Grid/L1/Power",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus system"
        },
        "pathObj": {
            "path": "/Ac/Grid/L1/Power",
            "type": "float",
            "name": "Grid L1 (W)"
        },
        "name": "",
        "onlyChanges": false,
        "x": 370,
        "y": 200,
        "wires": [
            [
                "eb8bf3be4bd80ae7"
            ]
        ]
    },
    {
        "id": "0559245366f6d5a4",
        "type": "victron-input-system",
        "z": "f58786176192b1b7",
        "service": "com.victronenergy.system/0",
        "path": "/Ac/Grid/L2/Power",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus system"
        },
        "pathObj": {
            "path": "/Ac/Grid/L2/Power",
            "type": "float",
            "name": "Grid L2 (W)"
        },
        "name": "",
        "onlyChanges": false,
        "x": 370,
        "y": 260,
        "wires": [
            [
                "eb8bf3be4bd80ae7"
            ]
        ]
    },
    {
        "id": "74a545740f24ba3f",
        "type": "victron-input-system",
        "z": "f58786176192b1b7",
        "service": "com.victronenergy.system/0",
        "path": "/Ac/Grid/L3/Power",
        "serviceObj": {
            "service": "com.victronenergy.system/0",
            "name": "Venus system"
        },
        "pathObj": {
            "path": "/Ac/Grid/L3/Power",
            "type": "float",
            "name": "Grid L3 (W)"
        },
        "name": "",
        "onlyChanges": false,
        "x": 370,
        "y": 320,
        "wires": [
            [
                "eb8bf3be4bd80ae7"
            ]
        ]
    }
]
